package com.example.examsystem.model;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 权限实体
 */
@Entity
@Table(name = "permissions")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class Permission {
    
    /**
     * 权限类型枚举
     */
    public enum PermissionType {
        /**
         * 菜单权限
         */
        MENU("菜单"),
        
        /**
         * 按钮权限
         */
        BUTTON("按钮"),
        
        /**
         * 接口权限
         */
        API("接口"),
        
        /**
         * 数据权限
         */
        DATA("数据");
        
        private final String displayName;
        
        PermissionType(String displayName) {
            this.displayName = displayName;
        }
        
        public String getDisplayName() {
            return displayName;
        }
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 权限名称
     */
    @Column(name = "name", nullable = false, unique = true, length = 100)
    private String name;
    
    /**
     * 权限显示名称
     */
    @Column(name = "display_name", length = 100)
    private String displayName;
    
    /**
     * 权限描述
     */
    @Column(name = "description", length = 500)
    private String description;
    
    /**
     * 权限资源
     */
    @Column(name = "resource", length = 100)
    private String resource;
    
    /**
     * 权限操作
     */
    @Column(name = "action", length = 50)
    private String action;
    
    /**
     * 权限类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "type", length = 20)
    private PermissionType type = PermissionType.API;
    
    /**
     * 菜单路径（仅菜单类型权限使用）
     */
    @Column(name = "path", length = 200)
    private String path;
    
    /**
     * 组件路径（仅菜单类型权限使用）
     */
    @Column(name = "component", length = 200)
    private String component;
    
    /**
     * 图标（仅菜单类型权限使用）
     */
    @Column(name = "icon", length = 50)
    private String icon;
    
    /**
     * 排序
     */
    @Column(name = "sort")
    private Integer sort = 0;
    
    /**
     * 是否可见（仅菜单类型权限使用）
     */
    @Column(name = "visible")
    private Boolean visible = true;
    
    /**
     * 父级权限ID
     */
    @Column(name = "parent_id")
    private Long parentId;
    
    /**
     * 是否启用
     */
    @Column(name = "enabled", nullable = false)
    private Boolean enabled = true;
    
    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
}



